import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/views/home'
import {loadable} from "../utils/loadable";
import store from "../store";
import {CLEAN_REQUEST_TOKEN} from "../store/action-type";
import hooks from "./hooks";

Vue.use(VueRouter)

// 自动生成路由  不建议自动配置路由， 可配置性低，
const routes = [
    {
        path: '/',
        name: 'home',
        component: Home
    },
    {
        path: '/lesson',
        name: 'lesson',
        component: loadable(() => import('@/views/lesson')), // 动态加载组件 添加loading
        meta: {
            needLogin: true, // 需要登录
        }
    },
    {
        path: '/profile',
        name: 'profile',
        component: loadable(() => import('@/views/profile')),
        meta: {
            needLogin: true, // 需要登录
        }
    },
    {
        path: '/login',
        name: 'login',
        component: loadable(() => import('@/views/login'))
    },
    {
        path: '/register',
        name: 'register',
        component: loadable(() => import('@/views/reg'))
    }
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

Object.values(hooks).forEach((hook) => {
    router.beforeEach(hook)
})

export default router
